package com.hyz.service.impl;

import client.AssetClient;
import com.hyz.pojo.request.ApprovalResultRequest;
import com.hyz.pojo.response.AssetMaintenanceResponse;
import com.hyz.pojo.response.AssetRetirementResponse;
import com.hyz.pojo.response.AssetSecondmentResponse;
import com.hyz.service.OperationsService;
import com.hyz.util.AssetMaintenanceUtils;
import com.hyz.util.Conversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.ResponseResult;
import pojo.asset.assetMaintenance.AssetMaintenance;
import pojo.asset.assetMaintenance.AssetMaintenanceResult;
import pojo.asset.assetRetirement.AssetRetirement;
import pojo.asset.assetRetirement.AssetRetirementResult;
import pojo.asset.assetSecondment.AssetSecondment;
import pojo.asset.assetSecondment.AssetSecondmentResult;

import java.util.List;
import java.util.Objects;

/**
 * @author HGD
 * @date 2022/10/10 15:45
 */
@Service
public class OperationsServiceImpl implements OperationsService {
    private final int code = 200;
    @Autowired
    private AssetClient assetClient;
    @Autowired
    private Conversion conversion;
    @Autowired
    private AssetMaintenanceUtils amUtils;

    //----------------------------------------------------------------------------------------------------------
    // 资产借调

    @Override
    public ResponseResult<List<AssetSecondmentResponse>> opGetAssetSecondments() {
        ResponseResult<List<AssetSecondment>> result = assetClient.opGetAssetSecondments();
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        List<AssetSecondmentResponse> responses = conversion.getAssetSecondmentResponse(result.getData());
        return new ResponseResult<>(result.getStatus(), result.getMsg(), responses);
    }

    @Override
    public ResponseResult<List<AssetSecondmentResponse>> opUpdateAssetSecondments(ApprovalResultRequest request) {
        ResponseResult<String> result = assetClient.opUpdateAssetSecondments(request.getApprovalId(),
                request.getResult() ? AssetSecondmentResult.OPERATIONS_AGREE : AssetSecondmentResult.FAILED);
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        return opGetAssetSecondments();
    }

    //----------------------------------------------------------------------------------------------------------
    // 资产维修

    @Override
    public ResponseResult<List<AssetMaintenanceResponse>> opGetAssetMaintenance() {
        ResponseResult<List<AssetMaintenance>> result = assetClient.opGetAssetMaintenance();
        if (!Objects.equals(result.getStatus(), code)) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        List<AssetMaintenanceResponse> responses = conversion.getAssetMaintenanceResponse(result.getData());
        return new ResponseResult<>(result.getStatus(), result.getMsg(), responses);
    }


    @Override
    public ResponseResult<List<AssetMaintenanceResponse>> opUpdateAssetMaintenance(ApprovalResultRequest request) {
        ResponseResult<Boolean> result = assetClient.opUpdateAssetMaintenance(request.getApprovalId(),
                request.getResult() ? AssetMaintenanceResult.OPERATIONS_SUCCESS : AssetMaintenanceResult.OPERATIONS_FAILED);
        if (!result.getData()) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        return opGetAssetMaintenance();
    }

    //----------------------------------------------------------------------------------------------------------
    // 资产报废

    @Override
    public ResponseResult<List<AssetRetirementResponse>> opGetAssetRetirement() {
        int code = 200;
        ResponseResult<List<AssetRetirement>> result = assetClient.opGetAssetRetirement();
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        List<AssetRetirementResponse> responses = conversion.getAssetRetirementResponse(result.getData());
        return new ResponseResult<>(result.getStatus(), result.getMsg(), responses);
    }

    @Override
    public ResponseResult<List<AssetRetirementResponse>> opUpdateAssetRetirement(ApprovalResultRequest request) {
        ResponseResult<Boolean> result = assetClient.opUpdateAssetRetirement(request.getApprovalId(),
                request.getResult() ? AssetRetirementResult.OPERATIONS_AGREE : AssetRetirementResult.FAILED);
        if (!result.getData()) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        return opGetAssetRetirement();
    }
}
